﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TransitFeedGenerator;
using TransitFeedGenerator.Attribute;
using TransitFeedGenerator.Entity;
using TransitFeedGenerator.Enum;
using TransitFeedGenerator.Util;

namespace TFG_Test
{
    [TestClass]
    public class UnitTest1
    {
        int tripId = 0;
        DateTime time = DateTime.Now;
        private Agency CreateAgency()
        {
            var objAgency = new Agency();
            objAgency.Id = "a1";
            objAgency.Name = "Test Agency";
            objAgency.TimeZone = TimeZones.AmericaFortaleza;
            return objAgency;
        }

        private  List<Agency> CreateAgencies()
        {
            var objAgency = new Agency();
            objAgency.Id = "a1";
            objAgency.Name = "Test Agency";
            objAgency.TimeZone = TimeZones.AmericaFortaleza;
            objAgency.Language = Languages.Portuguese;
            objAgency.Url = new Uri("http://localhost");
            var lstAux = new List<Agency>();
            lstAux.Add(objAgency);
            return lstAux;
        }

        private List<Routes> CreateRoutes()
         {
             var lstAux = new List<Routes>();
             lstAux.Add(new Routes(){Agency = CreateAgency(),Description ="Route #1", Id = "r1", LongName = "Teste Route 1",ShortName = "TR1",Type=RouteType.Bus,Url = new Uri("http://localhost")});
             lstAux.Add(new Routes() { Agency = CreateAgency(), Description = "Route #2", Id = "r2", LongName = "Teste Route 2", ShortName = "TR2", Type = RouteType.Bus, Url = new Uri("http://localhost") });
             lstAux.Add(new Routes() { Agency = CreateAgency(), Description = "Route #3", Id = "r3", LongName = "Teste Route 3", ShortName = "TR3", Type = RouteType.Bus, Url = new Uri("http://localhost") });
            return lstAux;
        }

        private List<Trip> CreateTrip(Routes route)
        {
            tripId++;
            var lstAux = new List<Trip>();
            lstAux.Add(new Trip() { Id = "V" + tripId, Direction = DirectionType.Inbound, Headsign = "V01" });
            tripId++;
            lstAux.Add(new Trip() { Id = "V" + tripId, Direction = DirectionType.Inbound, Headsign = "V02" });

            foreach (var item in lstAux)
            {
                item.StopTimes = CreateStopTime(item);   
            }

            return lstAux;
        }

        private List<StopTime> CreateStopTime(Trip trip)
        {
            var lstStops = CreateStops();
            var lstAux = new List<StopTime>();

            
            foreach (var item in lstStops)
            {
                lstAux.Add(new StopTime() { Stop = item, Arrival = time, Departure = time.AddMinutes(5)});
                time = time.AddMinutes(10);
            }
            
            return lstAux;
        }

        private List<Routes> CreateRoutesWithTrip()
        {
            var lstAux = new List<Routes>();
            lstAux.Add(new Routes() { Agency = CreateAgency(), Description = "Route #1", Id = "r1", LongName = "Teste Route 1", ShortName = "TR1", Type = RouteType.Bus, Url = new Uri("http://localhost")});
            lstAux.Add(new Routes() { Agency = CreateAgency(), Description = "Route #2", Id = "r2", LongName = "Teste Route 2", ShortName = "TR2", Type = RouteType.Bus, Url = new Uri("http://localhost") });
            lstAux.Add(new Routes() { Agency = CreateAgency(), Description = "Route #3", Id = "r3", LongName = "Teste Route 3", ShortName = "TR3", Type = RouteType.Bus, Url = new Uri("http://localhost") });

            lstAux[0].Trips = CreateTrip(lstAux[0]);
            lstAux[1].Trips = CreateTrip(lstAux[1]);
            lstAux[2].Trips = CreateTrip(lstAux[2]);
                        
            return lstAux;
        }

        private List<Stops> CreateStops()
        {
            var lstAux = new List<Stops>();
            lstAux.Add(new Stops() { Code = "S1", Description = "Stop #1", Id = "S1", Latitude = "-3.808694000000001", Longitude = "-38.543483",Name = "Bus Stop #1",Type = LocationType.Stop,Url = new Uri("http://localhost")});
            lstAux.Add(new Stops() { Code = "S2", Description = "Stop #2", Id = "S2", Latitude = "-3.812675", Longitude = "-38.535919", Name = "Bus Stop #2", Type = LocationType.Stop, Url = new Uri("http://localhost") });
            lstAux.Add(new Stops() { Code = "S3", Description = "Stop #3", Id = "S3", Latitude = "-3.815551999999999", Longitude = "-38.539959", Name = "Bus Stop #3", Type = LocationType.Stop, Url = new Uri("http://localhost") });
            lstAux.Add(new Stops() { Code = "S4", Description = "Stop #4", Id = "S4", Latitude = "-3.814684", Longitude = "-38.536656", Name = "Bus Stop #4", Type = LocationType.Stop, Url = new Uri("http://localhost") });
            lstAux.Add(new Stops() { Code = "S5", Description = "Stop #5", Id = "S5", Latitude = "-3.810285", Longitude = "-38.534651", Name = "Bus Stop #5", Type = LocationType.Stop, Url = new Uri("http://localhost") });
            lstAux.Add(new Stops() { Code = "S6", Description = "Stop #6", Id = "S6", Latitude = "-3.803185", Longitude = "-38.538487", Name = "Bus Stop #6", Type = LocationType.Stop, Url = new Uri("http://localhost") });
            return lstAux;
        }


        private Schedule CreateSchedule()
        {
            var lstWeek = new List<DayOfWeek>();
            lstWeek.Add(DayOfWeek.Monday);
            lstWeek.Add(DayOfWeek.Sunday);
            lstWeek.Add(DayOfWeek.Friday);
            return new Schedule() { End = DateTime.Parse("31/12/2011"), Id = "C1", Start = DateTime.Parse("01/01/2011"), WeekDaysValids = lstWeek };
            
        }

        [TestMethod]
        public void Can_Convert_A_TimeZoneEnum_in_a_String_Value()
        {
            var objAgency = CreateAgency();
            Assert.AreEqual("America/Fortaleza", objAgency.TimeZone.GetEnumName<TimeZones>());
        }

        [TestMethod]
        public void Can_Map_Transif_Feed_to_Google_Transit_Entities()
        {
            var objFeed = new TransitFeed();

            objFeed.Agency = CreateAgencies();
            objFeed.Stops = CreateStops();
            objFeed.Schedules = CreateSchedule();
            objFeed.Schedules.Routes = CreateRoutesWithTrip();



            var result = FeedService.Generate(objFeed);
            FeedService.SaveToFile(@"d:\feed_test.zip", result);
            Assert.Inconclusive();
        }
    }
}
